import http from ".";

/**
 * 登录接口
 * @param data
 */
export const login = (data: { username: string; password: string }) =>
    http.post("/login", data);

/**
 * 角色列表
 */
export const fetchRoleList = () => http.get("/roles");

/**
 * 用户列表
 */
export const fetchUserList = () => http.get("/users");

/**
 * 权限列表
 */
export const fetchAclList = (type?: number) => http.get("/acls", { type });

/**
 * 添加用户
 * @param username
 * @param password
 * @param rid
 */
export const addUserItem = (data: UserFormInterface) =>
    http.post("/users", data);

/**
 * 修改用户
 * @param data
 */
export const updateUserItem = (data: UserFormInterface) => {
    const uid = data.uid;
    delete data.uid;
    return http.put(`/users/${uid}`, data);
};

/**
 * 添加一个角色
 * @param name
 */
export const addRoleItem = (data: RoleFormInterface) =>
    http.post("/roles", data);

/**
 * 添加一个权限
 * @param name
 */
export const addAclItem = (data: AclFormInterface) => http.post("/acls", data);

/**
 * 添加角色权限关系
 */
export const addRoleAclItem = (data: RoleAclFormInterface) =>
    http.post("/roles/acls", data);

/**
 * 获取角色权限列表
 */
export const fetchRoleAclList = (type: number) =>
    http.get("/roles/acls", { type });

/**
 * 添加用户表单数据
 */
export interface UserFormInterface {
    uid?: string | undefined;
    username: string;
    password: string;
    nickname: string;
    rid: string | undefined;
}

/**
 * 角色表单
 */
export interface RoleFormInterface {
    name: string;
}

/**
 * 权限表单
 */
export interface AclFormInterface {
    name: string;
    type: number;
    path?: string;
    method?: string | undefined;
}

/**
 * 角色权限表单
 */
export interface RoleAclFormInterface {
    rid: string | undefined;
    aid: string | undefined;
}
